package com.zhuannimei.ui;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.zhuannimei.R;
import com.zhuannimei.adapter.JoinListAdapter;
import com.zhuannimei.adapter.SimpleListAdapter;
import com.zhuannimei.dialog.SimpleListDialog;
import com.zhuannimei.dialog.SimpleListDialog.onSimpleListItemClickListener;
import com.zhuannimei.domain.JoinPeople;
import com.zhuannimei.net.RequestServerData;
import com.zhuannimei.parser.JoinListParser;
import com.zhuannimei.utility.WuLog;
import com.zhuannimei.utility.ZnmHttpParams;
import com.zhuannimei.view.ZnmRefreshListView;
import com.zhuannimei.view.ZnmRefreshListView.OnCancelListener;
import com.zhuannimei.view.ZnmRefreshListView.OnRefreshListener;

/**
 * @fileName JoinUsListActivity.java
 * @package com.zhuannimei.ui
 * @description 参与者列表
 * @author 大熊
 * @email 651319154@qq.com
 * @version 1.0
 * 
 */
public class JoinUsListActivity extends BaseActivity implements
		OnRefreshListener, OnCancelListener, OnItemClickListener {
	private static final String TAG = "JoinUsListActivity";
	private ZnmRefreshListView zl;
	public static final String LUCKID = "luckId";
	private String mLuckId;

	private List<JoinPeople> mList = new ArrayList<JoinPeople>();
	private JoinListAdapter mAdapter;
	private SimpleListDialog mListDialog;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.joinlist);
		mLuckId = getIntent().getStringExtra(LUCKID);
		mListDialog = new SimpleListDialog(this);
		mListDialog.setOnSimpleListItemClickListener(new onSimpleListItemClickListener() {
			@Override
			public void onItemClick(int position) {
				
			}
		});
		intiView();
		setListener();
		setData();
	}

	// 设置监听
	private void setListener() {
		zl.setOnRefreshListener(this);
		zl.setOnCancelListener(this);
		zl.setOnItemClickListener(this);
	}

	// 设置数据
	private void setData() {
		mAdapter = new JoinListAdapter(JoinUsListActivity.this, mList);
		zl.setAdapter(mAdapter);
		zl.onManualRefresh();
//		showCustomToast("正在查询");
		showLoadingDialog("正在查询");
		getJoinListFromNet();

	}

	@Override
	protected void onStart() {
		super.onStart();
		mTitlePane.setTitle("参与人列表");
		mTitlePane.getLeftButton().setVisibility(View.VISIBLE);
		mTitlePane.setLeftButtonTextAndListener("返回", new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
	}

	private void intiView() {
		zl = (ZnmRefreshListView) findViewById(R.id.point_shop_ZR);
	}

	private void getJoinListFromNet() {
		RequestParams mParams = ZnmHttpParams.getDefaultRequestParams();
		mParams.addBodyParameter("luckId", mLuckId);
		mParams.addBodyParameter("pageIndex", "0");
		RequestServerData.getJoinList(mParams, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				WuLog.i(TAG, "访问成功==" + responseInfo.result);
				try {
					List<JoinPeople> list = new JoinListParser()
							.parseJSON(responseInfo.result);
					WuLog.i(TAG, "集合的大小" + list.size());
					mList.clear();
					if (list != null && list.size() != 0) {
						mList.addAll(list);
					}
					mAdapter.notifyDataSetChanged();
				} catch (JSONException e) {
					e.printStackTrace();
				}
				zl.onRefreshComplete();
				dismissLoadingDialog();
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				WuLog.i(TAG, "访问失败error==" + error.getExceptionCode());
				showCustomToast("网络不给力");
				zl.onRefreshComplete();
				dismissLoadingDialog();
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		WuLog.i(TAG, "点击的位置==" + arg2);
		if (arg2 != 0) {
			JoinPeople jp = mList.get(arg2 - 1);
			// showCustomToast("购买的幸运码："+jp.getCodes());
			mListDialog.setTitle("" + jp.getUserName() + "购买的幸运码");
			mListDialog.setTitleLineVisibility(View.GONE);
			List<String> datas = getCodes(jp.getCodes());
			mListDialog.setAdapter(new SimpleListAdapter(
					JoinUsListActivity.this, datas));
			mListDialog.show();
		}
	}

	private List<String> getCodes(String codes) {
		List<String> mlist = new ArrayList<String>();
		String[] strs = codes.split(",");
		for (int x = 0; x < strs.length; x++) {
			mlist.add(strs[x]);
		}
		return mlist;
	}

	@Override
	public void onCancel() {
		zl.onRefreshComplete();
	}

	@Override
	public void onRefresh() {
		getJoinListFromNet();
	}
}
